package org.fei.demo;

import org.fei.demo.model.MqMsg;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;

/**
 * Hello world!
 * @author xff
 */
@SpringBootApplication
@RestController
public class DemoBootApp {

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/rabbit_demo")
    public String rabbitDemo(){
        System.out.println("========== Rabbit Demo ==========");

        MqMsg mqMsg = new MqMsg();
        mqMsg.setName("测试");
        mqMsg.setAge(30);
        mqMsg.setAmount(new BigDecimal("45.8"));
        mqMsg.setBirthDate(new Date());

        rabbitTemplate.convertAndSend("q.BOOT.RABBIT.TEST","boot_rabbit_test",mqMsg);

        return "Rabbit Demo.";
    }

    @RequestMapping("/redis_demo")
    public String redisDemo(){
        System.out.println("========== Redis Demo ==========");
        redisTemplate.opsForValue().set("k1","v1");
        System.out.println(redisTemplate.opsForValue().get("k1"));
        return "Redis Demo.";
    }

    @RequestMapping("/hello")
    public String hello(){
        return "Hello Spring Boot.";
    }

    public static void main( String[] args ) {
        SpringApplication.run(DemoBootApp.class);
        System.out.println( "Hello Spring Boot!" );
    }
}
